﻿using D.B.Data.API.Applications.Command;
using D.B.Data.Domain;
using D.B.Data.Domain.DTO.Output;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;
using System.Threading.Tasks;

namespace D.B.Data.API.Applications.CommandHandler
{
    public class QuerAdderssCommandHandler : IRequestHandler<QuerAdderssCommand, APIResult<List<AddressModel>>>
    {
        private readonly IBaseRepository<AddressModel> baseRepository;

        public QuerAdderssCommandHandler(IBaseRepository<AddressModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<APIResult<List<AddressModel>>> Handle(QuerAdderssCommand request, CancellationToken cancellationToken)
        {
            var res = baseRepository.GetAll().Where(s=>!s.IsDel&&s.Parent==request.Id);
           // var data =await GetAddress(request.Id, res.ToList());

            APIResult<List<AddressModel>> result = new APIResult<List<AddressModel>>()
            {
                 Code=EnumCode.Ok,
                 Data= res.ToList(),
                 Msg="显示成功"
            };

            return await Task.FromResult(result);
            
        }

        //public async Task<List<AddressDTO>> GetAddress(long id,List<AddressModel> model)
        //{
        //    var data = model.Where(s => s.Parent == id);

        //    List<AddressDTO> dTOs = new List<AddressDTO>();

        //    foreach(var item in data)
        //    {
        //        dTOs.Add(new AddressDTO
        //        {
        //            value = item.Id,
        //            label = item.AddressName,
        //            children = await GetAddress(item.Id, model)
        //        });
        //    }


        //    return dTOs;
            
        //}
    }
}
